﻿Imports DataAccess
Imports Entities
Public Class ClienteBL

    Public Shared Function ListByCalleId(ByVal zId As Long) As List(Of CLIENTES)
        Dim lista As New List(Of CLIENTES)
        lista = ClienteDal.ListByCalleId(zId)
        Return lista
    End Function

    Public Shared Function ListByNumero(ByVal zNumero As String, ByVal zcalle As Long) As List(Of CLIENTES)
        Dim lista As New List(Of CLIENTES)
        Dim listaF As New List(Of CLIENTES)
        lista = ListByCalleId(zcalle)
        If Not IsNothing(lista) Then
            listaF = lista.Where(Function(e) e.NUMERO.Contains(zNumero))
        End If
        Return listaF
    End Function

    Public Shared Function List() As List(Of CLIENTES)
        Dim lista = ClienteDal.List
        Return lista
    End Function
   
    Public Shared Function Save(ByVal zobject As CLIENTES) As CLIENTES
        If zobject.RUT = "  .   .   -" Then zobject.RUT = ""
        If ClienteDal.Existe(zobject.CLIENTE) Then
            Return ClienteDal.Update(zobject)
        Else
            Return ClienteDal.Create(zobject)
        End If
    End Function

    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Return ClienteDal.delete(zId)
    End Function

    Public Shared Function SearchByDireccion(ByVal zNumero As String, ByVal zCalle As Long) As CLIENTES
        Dim cliente As New CLIENTES
        cliente = ClienteDal.SearchByDireccion(zNumero, zCalle)
        Return cliente
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As CLIENTES
        Dim obj = ClienteDal.SearchById(zId)
        Return obj
    End Function
    Public Shared Function SearchByIdALL(ByVal zId As Long) As CLIENTES
        Dim obj = ClienteDal.SearchByIdALL(zId)
        Return obj
    End Function


    Public Shared Function ListP() As List(Of ClientesP)
        Dim lista = List()
        Dim listaF As New List(Of ClientesP)
        For Each cli In lista
            listaF.Add(ClientesToClientesP(cli))
        Next
        If lista.Count > 0 Then
            Return listaF
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function ClientesToClientesP(ByVal zCliente As CLIENTES) As ClientesP


        Dim cliP As New ClientesP
        Dim cal As New CALLES
        cal = CalleBL.SearchById(zCliente.CALLE)
        If IsNothing(cal) Then
            cal.CALLE = ""
        Else
            cliP.Calle = cal.NOMBRE
            cliP.Villa = VillaBL.GetNombre(cal.VILLA)
        End If
        cliP.ClienteId = zCliente.CLIENTE
        cliP.Numero = zCliente.NUMERO
        cliP.Nombre = zCliente.NOMBRE
        cliP.Fono = zCliente.FONO
        cliP.Rut = zCliente.RUT

        Return cliP

    End Function
    Public Shared Function ListHistorial(ByVal zCLienteId) As List(Of HistorialClienteR)
        Dim ventasList As New List(Of VENTAS)
        ventasList = VentaDAL.ListByCLienteId(zCLienteId)
        Dim ListaHistorial As New List(Of HistorialClienteR)
        For Each venta In ventasList
            Dim detalle As New List(Of DETALLEVENTA)
            detalle = DetalleVentaDAL.ListByVentaIdId(venta.VENTA)

            'Dim cliente = ClienteBL.SearchById(venta.CLIENTE)
            'Dim calle = CalleBL.SearchById(cliente.CALLE)
            Dim chofer = ChoferBL.SearchById(venta.RUT)
            Dim camion = CamionBL.SearchById(venta.PATENTE)
            Dim boleta = BoletaBL.SearchById(venta.BOLETA)
            Dim pagolist = PagosBL.ListByVentaId(venta.VENTA)


            Dim hr As New HistorialClienteR
            With hr
                .Fecha = CDate(venta.FECHA).ToShortDateString
                .Hora = CDate(venta.FECHA).ToShortTimeString
                .Observacion = boleta.DESCRIPCION
                .Camion = camion.DESCRIPCION
                .Chofer = chofer.NOMBRES
                .Producto = ""
                .Total = FormatCurrency(boleta.TOTAL)
            End With

            For Each ven In detalle
                With hr
                    If .Producto.Length > 0 Then .Producto += ", "
                    If ven.ITEM > 0 Then
                        .Producto += ItemBL.GetDescripcion(ven.ITEM)
                    End If
                    If ven.GARANTIA > 0 Then
                        .Producto += GarantiaBL.GetDescripcion(ven.GARANTIA)
                    End If
                End With
            Next
            For i As Integer = 0 To pagolist.Count - 1
                If i = 0 Then
                    Dim formPago = FormaPagoBL.SearchById(pagolist(i).FORMAPAGO)
                    hr.Monto = pagolist(i).MONTO
                    hr.FormaPago = formPago.DESCRIPCION
                    ListaHistorial.Add(hr)
                Else
                    Dim hr2 As New HistorialClienteR
                    With hr2
                        Dim formPago = FormaPagoBL.SearchById(pagolist(i).FORMAPAGO)
                        .Fecha = ""
                        .Hora = ""
                        .Observacion = ""
                        .Camion = ""
                        .Chofer = ""
                        .Producto = ""
                        .Total = ""
                        .Monto = pagolist(i).MONTO
                        .FormaPago = formPago.DESCRIPCION
                    End With
                    ListaHistorial.Add(hr2)
                End If
            Next
            'ListaHistorial.Add(hr)
        Next
        Return ListaHistorial
    End Function

End Class
